package com.ruoyi.web.controller.classes.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.web.controller.classes.mapper.AClassesMapper;
import com.ruoyi.web.controller.classes.domain.AClasses;
import com.ruoyi.web.controller.classes.service.IAClassesService;

/**
 * 课程管理Service业务层处理
 *
 * @author G
 * @date 2025-04-28
 */
@Service
public class AClassesServiceImpl implements IAClassesService
{
    @Autowired
    private AClassesMapper aClassesMapper;

  @Value("${ruoyi.profile}")
  private String profile;
  /**
   * 查询课程管理
   *
   * @param classId 课程管理主键
   * @return 课程管理
   */
  @Override
  public AClasses selectAClassesByClassId(Long classId) {
    return aClassesMapper.selectAClassesByClassId(classId);
  }

    /**
     * 查询课程管理列表
     *
     * @param aClasses 课程管理
     * @return 课程管理
     */
    @Override
    public List<AClasses> selectAClassesList(AClasses aClasses)
    {
        return aClassesMapper.selectAClassesList(aClasses);
    }

    /**
     * 新增课程管理
     *
     * @param aClasses 课程管理
     * @return 结果
     */
    @Override
    public int insertAClasses(AClasses aClasses)
    {
        return aClassesMapper.insertAClasses(aClasses);
    }

    /**
     * 修改课程管理
     *
     * @param aClasses 课程管理
     * @return 结果
     */
    @Override
    public int updateAClasses(AClasses aClasses)
    {
        return aClassesMapper.updateAClasses(aClasses);
    }

    /**
     * 批量删除课程管理
     *
     * @param classIds 需要删除的课程管理主键
     * @return 结果
     */
    @Override
    public int deleteAClassesByClassIds(Long[] classIds)
    {
        return aClassesMapper.deleteAClassesByClassIds(classIds);
    }

    /**
     * 删除课程管理信息
     *
     * @param classId 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteAClassesByClassId(Long classId)
    {
        return aClassesMapper.deleteAClassesByClassId(classId);
    }

    /**
     * 按照登陆老师查询课程管理列表
     * @param aClasses
     * @return
     */
    @Override
    public List<AClasses> selectAClassesList_t(AClasses aClasses) {
        return aClassesMapper.selectAClassesList_t(aClasses);
    }

  @Override
  public List<Map<String, Object>> selectAClassesList_t_all() {
    return aClassesMapper.selectAClassesList_t_all();
  }

  /**
   * 拿到数据库里面对应课程id的考试题，按照题干，选项（这个是list里面套着list，如果是填空题这里对应的值是[]），
   * 答案的方式储存
   */
  @Override
  public List<Map<String, Object>> getExam(Long classId) {
    AClasses aClasses = new AClasses();
    aClasses.setClassId(classId);
    //获取这个课程的试题地址
    String examAddress = profile+aClassesMapper.selectAClassesList(aClasses).get(0).getClassExam();
    String newPath = examAddress.replace("/profile", "");
    //System.out.println("examAddress = " + examAddress);
    List<Map<String, Object>> examList = new ArrayList<>();

    try (FileInputStream fis = new FileInputStream(newPath);
         XWPFDocument document = new XWPFDocument(fis)) {

      List<XWPFParagraph> paragraphs = document.getParagraphs();
      List<String> timu_list = new ArrayList<>();
      List<String> xuanxiang_list =  new ArrayList<>();
      List<String> daan_list =  new ArrayList<>();

      List<List<Integer>> key = new ArrayList<>();
      for (int i = 0; i < paragraphs.size(); i++) {
        String line = paragraphs.get(i).getText();
        //System.out.println("line = " + line);
      }
      for (int i = 0; i < paragraphs.size(); i++) {
        String line = paragraphs.get(i).getText();
        if (line.length()>=2){
          if (line.substring(0,2).equals("题目")){
            List<Integer> key_sun = new ArrayList<>();
            key_sun.add(i);
            key_sun.add(1);
            key.add(key_sun);
            continue;
          }
          if (line.substring(0,2).equals("选项")){
            List<Integer> key_sun = new ArrayList<>();
            key_sun.add(i);
            key_sun.add(2);
            key.add(key_sun);
            continue;
          }
          if (line.substring(0,2).equals("答案")){
            List<Integer> key_sun = new ArrayList<>();
            key_sun.add(i);
            key_sun.add(3);
            key.add(key_sun);
            continue;
          }
        }else{

        }
      }
      //题目，选项，答案类型
      //System.out.println("key = " + key);
      for (int i = 0; i < key.size()-1; i++) {
        //当前类型和下一个类型的索引
       int l = key.get(i).get(0);
       int r =key.get(i+1).get(0);

       //当前类型的种类
       int keyl=key.get(i).get(1);
        //System.out.println("keyl = " + keyl);
       int keyr=key.get(i+1).get(1);
        //System.out.println("keyr = " + keyr);
       if (keyl==1 && keyr==2){
         //题目
         String timu_str="";
         for (int j=l;j<r;j++){
           //System.out.println("题目"+paragraphs.get(j).getText());
           timu_str+=paragraphs.get(j).getText();
         }
         timu_list.add(timu_str);
       }else if (keyl==2 && keyr==3){
         //选择题选项
         String xuanxiang_str = "";
         for (int j=l;j<r;j++){
           //System.out.println("选项"+paragraphs.get(j).getText());
           xuanxiang_str+=paragraphs.get(j).getText();
         }
         xuanxiang_list.add(xuanxiang_str);
       }else if (keyl==3 && keyr==1){
         //答案
         String daan_str="";
         for (int j=l;j<r;j++){
           //System.out.println("答案"+paragraphs.get(j).getText());
           daan_str+=paragraphs.get(j).getText();
         }
         daan_list.add(daan_str);
       }else if (keyl==1 && keyr==3){
         //填空题题目
         String timu_str="";
         for (int j=l;j<r;j++){
           timu_str+=paragraphs.get(j).getText();
         }
         timu_list.add(timu_str);
         //给选项赋值
         xuanxiang_list.add("0");
       }
      }
      Map<String,Object> map = new HashMap();
      map.put("timu_list",timu_list);
      map.put("xuanxiang_list",xuanxiang_list);
      map.put("daan_list",daan_list);
//      System.out.println("timu_list = " + timu_list);
//      System.out.println("xuanxiang_list = " + xuanxiang_list);
//      System.out.println("daan_list = " + daan_list);
      examList.add(map);
    } catch (IOException e) {
      e.printStackTrace();
    }

    System.out.println("examList = " + examList);
    return examList;
  }

  @Override
  public String getClasses(Long classId) {
    return null;
  }

  @Override
  public List<Map<String,Object>> getFileList(Long classId) {
    List<Map<String,Object>> fileList = aClassesMapper.getFileList(classId);

    return fileList;
  }
}
